var data = {
    value: 1,
    children: [{
        value: 2,
        children: [{
            value: 3,
            children: [{
                value: 4
            }, {
                value: 4
            }, {
                value: 4
            }, {
                value: 4
            }]
        }, {
            value: 3,
            children: [{
                value: 4
            }, {
                value: 4
            }, {
                value: 4
            }, {
                value: 4
            }]
        }, {
            value: 3
        }, {
            value: 3
        }]
    }, {
        value: 2,
        children: [{
            value: 3
        }, {
            value: 3
        }, {
            value: 3
        }, {
            value: 3
        }]
    }]
}


function buildData(i, level) {
    level = i.level = level || 1
    if(Array.isArray(i)) {
        i.forEach(item => {
            buildData(item, level)
        })
        return
    }
    
    if('children' in i) {
        level += 1
        buildData(i['children'], level)
    }
}

buildData(data)
console.log(data)